﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ueEngine.Biz.Core.ExServer.FullSearch.Core;
using System.Diagnostics;
using ueEngine.Framework.Common;

namespace ueEngine.Biz.Core.ExServer.WorkPool.WorkHandle.FullSearch
{
    public abstract class IndexHandle<T> : IWorkHandle<T>
    {
        #region IWorkHandle<T> 成员

        public abstract WaitWorkItem Process(T context);

        #endregion
    }
    public class IndexPositionHandle<T> : IndexHandle<T>
    {
        #region 已注销
        //protected static List<PositionCell> ArticleCache = new List<PositionCell>();
        ///// <summary>
        ///// 每300个单位执行一次
        ///// </summary>
        //protected static int cacheSize = 300;
        //public static int CacheSize
        //{
        //    get
        //    {
        //        return cacheSize;
        //    }
        //}
        //private static int period = 1000 * 60 * 60; //一个小时执行一次
        //static Action doIndex = () =>
        //{
        //    var item = DoPrepareIndex(CloneList(ArticleCache));
        //    WorkPool.AddNewTask(item.Action, item.Context);
        //};

        //static IndexPositionHandle()
        //{
        //    System.Threading.Timer timer = new Timer(
        //        o =>
        //        {
        //            doIndex.Invoke();
        //        }, null, period, period);
        //}

        //public static int CurrentCacheSize
        //{
        //    get
        //    {
        //        Monitor.Enter(ArticleCache);
        //        int itemCount = ArticleCache.Count;
        //        Monitor.Exit(ArticleCache);
        //        return itemCount;
        //    }
        //}
        #endregion

        public override WaitWorkItem Process(T context)
        {
                //PositionCell cell = new PositionCell(context as PositionItem);
            try
            {
                var data =  context as List<PositionItem>;
                WaitWorkItem waitworkItem = DoPrepareIndex(data.Select(c => new PositionCell(c)).ToList());
                return waitworkItem;
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                return new WaitWorkItem()
                {
                    IsDo = false
                };
            }
        }
        #region 已注销
        //        private static List<PositionCell> CloneList(List<PositionCell> list)
//        {
//#if DEBUG
//            StringBuilder sb  =new StringBuilder();
//            sb.Append("IndexPositionHandle.CloneList at :");
//            sb.Append(DateTime.Now.ToString());
//            sb.AppendLine(" invoking!");
//            sb.Append("ArticleCache hashCode:");
//            sb.Append(ArticleCache.GetHashCode());
//            sb.Append(" itemcount:");
//            sb.AppendLine(CurrentCacheSize.ToString());
//            LogHelper.Write(CommonLogger.FullSearch, LogLevel.Trace, sb.ToString());
//#endif
//            Monitor.Enter(ArticleCache);
//            //将缓存复制一份，然后清空
//            List<PositionCell> ectype = new List<PositionCell>(ArticleCache);
//            ArticleCache.Clear();
//            Monitor.Exit(ArticleCache);
//            return ectype;
        //        }
        #endregion
        /// <summary>
        /// 执行创建索引
        /// </summary>
        public static WaitWorkItem DoPrepareIndex(List<PositionCell> ectype)
        {
            return new WaitWorkItem()
            {
                Context = ectype,
                Action = o => {
                    List<PositionCell> data = o as List<PositionCell>;
                    LogHelper.Write(CommonLogger.ExServer, LogLevel.Trace, "创建索引开始执行，需要执行总条数：" + data.Count);   
                    if (data != null && data.Count > 0)
                    {
                        var helper = new PositionIndex(301, 301);
                        helper.CreateIndex(helper.INDEX_DIR);
                        for (int i=0,len = data.Count;i<len;i++)
                        {
                            LogHelper.Write(CommonLogger.ExServer, LogLevel.Trace, "索引positionid:" + data[i].PositionId);
                            try
                            {
                                helper.IndexString(data[i]);
                            }
                            catch (Exception e)
                            {
                                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                            }
                        }
                        helper.Close();
                    }
                    LogHelper.Write(CommonLogger.ExServer, LogLevel.Trace, "创建索引执行完毕，一共执行条数：" + data.Count);
                }
            };
        }
    }
}
